package com.hz.ys_production.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.ys_production.bean.PresDailypaperNew;
import com.hz.ys_production.dbsql.DailyPaperSql;
import com.hz.ys_system.dao.QueryDao;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.ys_stock.dao.MakingsDao.java
 *     Description(说明)	:  原材料管理
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class DailyPaperNewDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询原材料信息
	 */
	public String SelDailypaper(HttpServletRequest request){
		
		
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(DailyPaperSql.SEL_DAILYPAPERNEW);
		//订单名称，任务单名称，机台名称 条件查询
		sbSQL.append(new QueryDao().QueryInfo(request , new SqlParameter("name","taskname",
				"machinename") , "=") );
		//时间条件查询
		sbSQL.append(new QueryDao().QueryDateInfo(request,"d.endtime","begin","end",true));
		
		sbSQL.append(" order by o.orderid desc,d.endtime desc");
		
		return sbSQL.toString();
	}
	/**
	 * @author 韩飞
	 * @time Nov 17, 2011	
	 * @param puser
	 * @return
	 * @throws Exception 
	 * 方法功能: 保存原材料
	 */
	public boolean DoSaveDailyPaper( PresDailypaperNew mak ) throws Exception {
		boolean ins = false;
		CommonDao dbd= new CommonDao();
		if(mak!=null){
			mak.setEndtime(DateUtil.getDateTime());
			//保存或更新 
			ins = dbd.insertinfor(mak);
			if(ins){
				List<Map<String, Serializable>> sqllist = new ArrayList<Map<String, Serializable>>();
				Map<String, Serializable> sqlmap = new HashMap<String, Serializable>();
				
				//判断如果出现补纸数量 相应进行库存剪纸
				if(mak.getCutnum()>0){
					sqlmap = new HashMap<String, Serializable>();
					sqlmap.put("sql", DailyPaperSql.UPD_STOCK_INFO);
					sqlmap.put("sqlParams", new SqlParameter(mak.getCutnum(),mak.getCutnum(),mak.getTaskid()));
					sqllist.add(sqlmap);
				}
				
				//执行更新机台排产任务表
				sqlmap = new HashMap<String, Serializable>();
				sqlmap.put("sql", DailyPaperSql.UPD_MACHINETASK_INFO);
				sqlmap.put("sqlParams", new SqlParameter("已完工",mak.getEndtime(),mak.getTaskid()));
				sqllist.add(sqlmap);
				//执行更新生产单
				sqlmap = new HashMap<String, Serializable>();
				sqlmap.put("sql", DailyPaperSql.UPD_TASK_INFO);
				sqlmap.put("sqlParams",  new SqlParameter("已完工",mak.getTaskid()));
				sqllist.add(sqlmap);
				//更新机台状态信息
				sqlmap = new HashMap<String, Serializable>();
				sqlmap.put("sql", DailyPaperSql.UPD_MACHINE_INFO);
				sqlmap.put("sqlParams", new SqlParameter("空闲",mak.getTaskid()));
				sqllist.add(sqlmap);
				
				ins = dbd.executeSQL(sqllist);
				
				Map map = (Map)dbd.queryObjectByPS(DailyPaperSql.SEL_TASK_COM, new SqlParameter(mak.getTaskid()),null);
//				判断订单下的所有生产单是否都完工
				if(map.get("ywg").equals(map.get("sy"))){
					ins = dbd.executeSQL(DailyPaperSql.UPD_ORDER_INFO, new SqlParameter("已完工",map.get("orderid")));
				}
			}
		}
		return ins;
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 修改原材料信息
	 */
	public boolean UpdDailypaper(PresDailypaperNew mak,HttpServletRequest request){
		CommonDao dbo= new CommonDao();
		String oldcutnum = CommonDao.isnull(request.getParameter("oldcutnum"));
		boolean ins = dbo.updateinfor(mak);
		//更新库存数量 
		if(ins && !oldcutnum.equals(mak.getCutnum()+"")){
			ins = dbo.executeSQL(DailyPaperSql.UPD_STOCK_NUM, new SqlParameter(oldcutnum,mak.getCutnum(),oldcutnum,mak.getCutnum(),mak.getTaskid()));
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 删除 生产日报
	 */
	public boolean Del_DailyPaper(HttpServletRequest request){
		String taskid = CommonDao.isnull(request.getParameter("taskid"));
		CommonDBDao dbo= new CommonDBDao();
		return dbo.executeSQL(DailyPaperSql.DEL_DAILYPAPERNEW_INFO, new SqlParameter(taskid));
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 根据 gid 查询 对应原材料信息
	 */
	public Map SelDailypaper_info(HttpServletRequest request) throws Exception {
		String did  = CommonDao.isnull(request.getParameter("did"));
		CommonDao dbd= new CommonDao();
		return (Map)dbd.queryObjectByPS(DailyPaperSql.SEL_DAILYPAPERNEW_INFO, new SqlParameter(did),null);
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 查询生产日报信息
	 */
	public List SelDailypaper(){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonList(DailyPaperSql.SEL_DAILYPAPERNEW);
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询生产单订单相关信息
	 */
	public Map SelTaskInfo(String taskid){
		CommonDBDao dbo= new CommonDBDao();
		return (Map)dbo.queryObjectByPS(DailyPaperSql.SEL_TASKINFONEW_INFO, new SqlParameter(taskid),null);
	}
}
